from docclass import getwords
from docclass import classifier
from docclass import naivebayes
from docclass import fisherclassifier
import os.path

def classify(test_sets, classifiers):
    test_result = {}
    for t_set in test_sets.keys():
        fn = test_sets[t_set]
        infile = open(fn, "r")
        while infile:
            line = infile.readline()
            if not line:
                break
            for clname in classifiers.keys():
                cl = classifiers[clname]
                result = cl.classify(line)
                test_result.setdefault(clname, {})
                test_result[clname].setdefault(t_set, [0,0])
                if result == t_set:
                    test_result[clname][t_set][1] += 1
                else:
                    test_result[clname][t_set][0] += 1
    return test_result

fisher_cl = fisherclassifier(getwords)
naive_cl = naivebayes(getwords)

fisher_cl.setdb("fisher.db")
naive_cl.setdb("naive.db")

test_sets = {"news":"testdata/news.txt", "weather":"testdata/weather.txt"}
classifiers = {"fisher":fisher_cl, "naive":naive_cl}

test_result = classify(test_sets, classifiers)
for clname in test_result.keys():
    print "Classifier: " + clname
    print "\tCategory\tRight\tWrong"
    for t_set in test_result[clname].keys():
        print "\t" + t_set + "\t" + str(test_result[clname][t_set][1]) +\
                "\t" + str(test_result[clname][t_set][0])
